package main

import (
	"fmt"
	"os/exec"
	"yit/base"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
)

type Config struct {
	MainWindow     fyne.Window
	ChildWindow    fyne.Window
	welcomeLayout  *fyne.Container
	CurrentVersion *widget.Label
	EditBtn        *widget.Button
}

var cfg Config

func main() {

	base.Inits()
	// sql.ConnectDB()

	a := app.New()
	//创建一个窗口
	w := a.NewWindow("Yit")
	cfg.MainWindow = w

	allLayout := cfg.makeUI()

	/* cfg.runCommond("cd D:/project/go/src/account-manage")
	cfg.runCommond("git log") */

	w.SetContent(allLayout)

	// 设置窗口大小,
	w.Resize(fyne.NewSize(800, 500))
	// 窗口居中
	w.CenterOnScreen()
	//设置主窗口
	w.SetMaster()

	//展示窗口 并运行程序
	w.ShowAndRun()
}

func (app *Config) makeUI() fyne.CanvasObject {

	//================== start 头部导航 ==================//
	layoutBtn := widget.NewButton("布局", func() {})
	headNavLayoutLeft := container.New(layout.NewHBoxLayout(), layoutBtn)

	resourceManagerBtn := widget.NewButton("资源管理器", func() {})
	resourceManagerBtn.OnTapped = app.openResourceManager
	commandLineBtn := widget.NewButton("命令行", func() {})
	pullBtn := widget.NewButton("拉取", func() {})
	pushBtn := widget.NewButton("推送", func() {})
	headNavLayoutRight := container.New(layout.NewHBoxLayout(), resourceManagerBtn, commandLineBtn, pullBtn, pushBtn)
	// headNavLayout := container.NewBorder(nil, nil, layoutBtn, layout.NewSpacer(), headNavLayoutRight)
	headNavLayout := container.NewBorder(nil, nil, nil, headNavLayoutRight, headNavLayoutLeft)
	//================== end 头部导航 ==================//

	//================== start 文件状态 ==================//
	//提交信息描述
	commitDescInput := widget.NewEntry()
	commitDescInput.SetPlaceHolder("请输入提交信息描述")
	commitDescInput.MultiLine = true //多行输入
	commitDescInput.Wrapping = 2     // 开启自动换行
	submitBtn := widget.NewButton("提交", func() {})
	asyncSubmitBtn := widget.NewButton("同步更改", func() {})
	fileStatusScroll := container.NewVScroll(container.NewVBox(
		widget.NewButton("Button (text only)", func() { fmt.Println("tapped text button") }),
		widget.NewButton("Button (text only)", func() { fmt.Println("tapped text button") }),
		commitDescInput,
		container.NewHBox(submitBtn, asyncSubmitBtn),
	))
	// 设置垂直滚动容器的最小高度
	fileStatusScroll.SetMinSize(fyne.NewSize(100, 450))
	fileStatusLayout := container.New(layout.NewVBoxLayout(), fileStatusScroll)

	//================== start 文件状态 ==================//

	bodyLayout := container.NewHSplit(widget.NewButton("左侧", func() {}), fileStatusLayout)
	bodyLayout.Offset = 0.23
	allLayout := container.New(layout.NewVBoxLayout(), headNavLayout, bodyLayout)
	return allLayout
}

// 打开资源管理器
func (app *Config) openResourceManager() {
	path := "C:\\Users\\YourUsername\\Documents" // 替换为你想打开的路径

	// 创建命令行参数，用于启动explorer.exe并打开指定路径
	cmd := exec.Command("explorer.exe", path)
	fmt.Println(cmd)
	cmd.Run()
	// 在Windows平台上执行系统调用
	/* if err := syscall.StartProcess(cmd.Path, cmd.Args, &syscall.ProcAttr{}); err != nil {
		log.Fatal(err)
	} */
}

func (app *Config) runCommond(commond string) {

	// 准备要执行的命令
	cmd := exec.Command("cmd", "/c", commond)
	fmt.Println(cmd)
	output, err := cmd.Output()
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(output)

	fmt.Println(string(output))
	cmd.Run()
}
